package com.zyn.rsgl.utils.commonResult;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor

public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    // 状态码
    private Integer code;
    // 提示信息
    private String message;
    // 数据内容
    private T data;
    // 总数据量
    private long total;
    // 扩展信息（可选）
    private Map<String, Object> ext;

    public R(Integer code, String message) {
        this(code, message, null);
    }

    public R(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public R(int success, String successMsg, T data, long total) {
        this.code = success;
        this.message = successMsg;
        this.data = data;
        this.total = total;
    }

    // 使用ResultCode接口的静态工厂方法
    public static <T> R<T> ok() {
        return new R<>(ResultCode.SUCCESS, ResultCode.SUCCESS_MSG, null);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(ResultCode.SUCCESS, ResultCode.SUCCESS_MSG, data);
    }

    public static <T> R<T> ok(T data, long total) {
        return new R<>(ResultCode.SUCCESS, ResultCode.SUCCESS_MSG, data, total);
    }

    public static <T> R<T> ok(String message, T data, long total) {
        return new R<>(ResultCode.SUCCESS, message, data, total);
    }

    public static <T> R<T> ok(Integer code, String message, T data, long total) {
        return new R<>(code, message, data, total);
    }

    public static <T> R<T> ok(String message, T data) {
        return new R<>(ResultCode.SUCCESS, message, data);
    }

    public static <T> R<T> ok(Integer code, String message, T data) {
        return new R<>(code, message, data);
    }

    public static <T> R<T> fail() {
        return new R<>(ResultCode.FAIL, ResultCode.FAIL_MSG, null);
    }

    public static <T> R<T> fail(String message) {
        return new R<>(ResultCode.FAIL, message, null);
    }

    public static <T> R<T> fail(Integer code, String message) {
        return new R<>(code, message, null);
    }

}